//
//  CLActionCenter.m
//  ComponentLinkDemo
//
//  Created by zhenby on 2017/2/24.
//  Copyright © 2017年 zhenby. All rights reserved.
//

#import "CLActionCenter.h"
#import "CLPActionObserver.h"
#import "CLPComponent.h"
#import "CLPSafeMutableDictionary.h"

@interface CLActionInfo : NSObject

@property (nonatomic,weak) id<CLPComponent> observerComponent;
@property (nonatomic,assign) BOOL isObserverd;

@end

@implementation CLActionInfo

@end

@interface CLActionCenter ()

@property (nonatomic,weak) id<CLPComponent> rootComponent;

@property(nonatomic,strong) CLPSafeMutableDictionary* clObserverComponent;

@end

@implementation CLActionCenter

- (instancetype)initWithRootComponent:(id<CLPComponent>)rootComponent
{
    self = [super init];
    if (self) {
        _rootComponent = rootComponent;
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _clObserverComponent = [[CLPSafeMutableDictionary alloc] init];
    }
    return self;
}

- (BOOL)registerWithComponentId:(NSString*)componentId andActionObserver:(id<CLPActionObserver>)actionObserver
{
    if ([self.clObserverComponent objectForKey:componentId]) {
        NSLog(@"register actionObserver is existed %@",componentId);
        return NO;
    }
    
    if (actionObserver && [actionObserver conformsToProtocol:@protocol(CLPActionObserver)]) {
//        CLActionInfo*
    }
    return YES;
}

#pragma mark - CLPActionPerformerDelegate
- (void)postActionEvent:(nonnull CLActionEvent *)action
{
    [self dispatchActionEvent:action toComponent:self.rootComponent];
}

- (void)dispatchActionEvent:(CLActionEvent *)action toComponent:(id<CLPComponent>)component
{
    if (!component) {
        return;
    }

    if ([(id<CLPComponent>)component respondsToSelector:@selector(actionObserverWithEvent:)]) {
        [(id<CLPActionObserver>)component actionObserverWithEvent:action];
    }
    
    for (id<CLPComponent> actionObserver in component.subComponents ) {
        [self dispatchActionEvent:action toComponent:actionObserver];
    }
}

//- (BOOL)performAction:(CLActionContext*)actionContext
//{
//    if (!self.rootComponent) {
//        return NO;
//    }
//    
//    BOOL actionWasHandled = NO;
//    if ([(NSObject*)self.rootComponent conformsToProtocol:@protocol(CLPActionObserver)]) {
//        [(id<CLPActionObserver>)self.rootComponent actionObserverWithContext:actionContext];
//        actionWasHandled = YES;
//    }
//    
//    for (id<CLPComponent> actionObserver in self.rootComponent.subComponents ) {
//        if ([(NSObject*)actionObserver conformsToProtocol:@protocol(CLPActionObserver)]) {
//            [(id<CLPActionObserver>)actionObserver actionObserverWithContext:actionContext];
//            actionWasHandled = YES;
//        }
//    }
//    
//    return actionWasHandled;
//}
//
//- (void)ayncPerformAction:(CLActionContext*)actionContext complation:(action_complate_t)complate
//{
//    if (!self.rootComponent) {
//        if (complate) {
//            complate(NO);
//        }
//        return;
//    }
//
//    __weak typeof(self) weakSelf = self;
//    dispatch_async(dispatch_get_global_queue(0, 0), ^{
//        BOOL actionWasHandled = NO;
//        if ([(NSObject*)weakSelf.rootComponent conformsToProtocol:@protocol(CLPActionObserver)]) {
//            [(id<CLPActionObserver>)self.rootComponent actionObserverWithContext:actionContext];
//            actionWasHandled = YES;
//        }
//        
//        for (id<CLPComponent> actionObserver in weakSelf.rootComponent.subComponents ) {
//            if ([(NSObject*)actionObserver conformsToProtocol:@protocol(CLPActionObserver)]) {
//                [(id<CLPActionObserver>)actionObserver actionObserverWithContext:actionContext];
//                actionWasHandled = YES;
//            }
//        }
//        
//        if (complate) {
//            complate(actionWasHandled);
//        }
//    });
//}

@end
